package com.beyondsoft.sabg.edu.controller;

import com.beyondsoft.sabg.edu.model.dto.PrivilegeDTO;
import com.beyondsoft.sabg.edu.model.dto.UserAccountDTO;
import com.beyondsoft.sabg.edu.model.exception.InvalidParamException;
import com.beyondsoft.sabg.edu.model.vo.core.SimpleResponseVO;
import com.beyondsoft.sabg.edu.service.core.UserAccountService;
import com.beyondsoft.sabg.edu.utils.PageUtils;
import com.beyondsoft.sabg.edu.utils.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 *  系统设置管理
 * @Author Mo cr
 * @Date 2023/8/4
 */
@RestController
@RequestMapping("/api")
public class UserController {
    @Autowired
    private UserAccountService userAccountService;

    @PostMapping("/login")
    public SimpleResponseVO login(@RequestBody UserAccountDTO userAccountDTO) {
        return ResponseUtil.success().withData(userAccountService.login(userAccountDTO));
    }

    @GetMapping("/user")
    public SimpleResponseVO getUser() {
        return ResponseUtil.success().withData(userAccountService.getCurrentUser());
    }

    @GetMapping("/privilege/tree")
    public SimpleResponseVO getPrivilegeTree() {
        return ResponseUtil.success().withData(userAccountService.getPrivilegeTree());
    }

    @GetMapping("/user/privilege")
    public SimpleResponseVO getUserPrivilege(@RequestParam(name = "id") Long id) {
        if (id == null) {
            throw new InvalidParamException("id is invalid");
        }
        return ResponseUtil.success().withData(userAccountService.getRolePrivilege(id));
    }

    @GetMapping("/role/list")
    public SimpleResponseVO getRoleList() {
        return ResponseUtil.success().withData(userAccountService.getRoleList());
    }


    @DeleteMapping("/role")
    @PreAuthorize("hasRole('ADMIN')")
    public SimpleResponseVO deleteRoleById(@RequestParam(name = "roleId") Long roleId){
        userAccountService.deleteRoleById(roleId);
        return ResponseUtil.success();
    }
    @PatchMapping("/update/role")
    @PreAuthorize("hasRole('ADMIN')")
    public SimpleResponseVO updateUserRoles(@RequestBody UserAccountDTO userAccountDTO) {
        userAccountService.updateRole(userAccountDTO);
        return ResponseUtil.success();
    }


    @PostMapping("/privilege/save")
    @PreAuthorize("hasRole('ADMIN')")
    public SimpleResponseVO savePrivilegeTree(@RequestBody PrivilegeDTO privilegeDTO) {
        userAccountService.savePrivilege(privilegeDTO);
        return ResponseUtil.success();
    }

    @PostMapping("/user/filter")
    @PreAuthorize("hasRole('TEACHER')||hasRole('ADMIN')")
    public SimpleResponseVO getUserByFilter(@RequestBody UserAccountDTO userAccountDTO) {
        Pageable pageable = PageUtils.initPage(userAccountDTO);
        return ResponseUtil.success().withData( userAccountService.findAll(userAccountDTO,pageable));
    }


}
